import {ConfigDataEntity} from "../../../../../generate/entitas";
import BaseGenerator from "../../BaseGenerator";
import {TsGenerator} from "../../../../code_generater/ts/TsGenerator";
import GeneratorDecorator from "../../../GeneratorDecorator";
import MethodInfo from "../../../../MethodInfo";
import {CellData} from "../../../../CellData";
__dirname = __dirname.replace("/dist", "/src")


@GeneratorDecorator.generator("index", "ts")
export default class SingleIndex extends BaseGenerator{

	beforeInit() {
		this.dirPath = __dirname
	}

	onInit() {
		this.rootEntity.onGenerateHeadDataAction.value.push(this.onWriteHeadData.bind(this))
		this.rootEntity.onGenerateCodeAction.value.push(this.onWriteCode.bind(this))
	}

	onWriteHeadData(generator : TsGenerator , entity : ConfigDataEntity) {
		let indexDict = new Map()
		let rowValList = this.rootEntity.rowCellList.value
		let column = this.columnEntity.columnIndex.value
		for (let i = 0; i < rowValList.length; i++) {
			var row :CellData = rowValList[i]
			if (indexDict.has(row[column].value)){
				utils.LogErrorWithColumnEntity(`[index] index重复, 重复项:${row[column].value}, 文件:${row[column].fullName}`,this.columnEntity)
			}
			indexDict.set(row[column].value, row[0].value)
		}
		entity.headInfo.value.set(this.columnEntity.memberHeadData.value.name + "Index", indexDict)
	}

	getInitMethodName(){
		return `Init${this.columnEntity.memberHeadData.value.name}Index`
	}

	onWriteCode(generator : TsGenerator, entity : ConfigDataEntity){
		let ret = this.generateWithFile("template",
			{
				initMethodName:this.getInitMethodName(),
				dataKeyType : this.columnEntity.memberHeadData.value.normalType,
				dataKeyName : this.columnEntity.memberHeadData.value.name
			})
		var methodInfo = new MethodInfo()
		methodInfo.initMethodName = this.getInitMethodName()
		methodInfo.methodContent = ret
		let methodMap = entity.indexInitMethodNameDict.value
		methodMap.set(this.getInitMethodName(), methodInfo)
	}





}